
var netUtil = require('/../../utils/netUtil.js')
var util = require('/../../utils/util.js')
var app = getApp();

Page({

  data: {
     accountIds: '',//单据号拼接
     accountAccess : 0,//入库费用
     accountStorage : 0,//存储费用
     totalPrice : 0,//总价格
    disposablePrice:0
  },
  onLoad: function (options) {

   

    
  app.getUserOpedId(this.countDebtAccount);
  

  },
  onReady: function () {

  },
  onShow: function () {

    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },

  onUnload: function () {
    // 页面关闭

  },

  pay: function (e) {
    var that = this;

    netUtil.showAlertDialog("提示", '确认支付?', true, "确定", null, function () {
      var params = {
        openId: app.globalData.userInfo.openid,
        data: {
          accountCode: that.data.accountIds
        }
      };

      netUtil.buildRequest(that, '/wisen/wechat/wxPay', params, {
        onPre: function () {
          netUtil.showLoadingDialog(that);
        },
        onSuccess: function (resp) {

          netUtil.hideLoadingDialog(that);

          //如果请求成功
          if (resp.ret == app.globalData.respSuccessCode) {

            var data = resp.data;

            wx.requestPayment({
              'appId': data.appid,
              'timeStamp': data.timeStamp,
              'nonceStr': data.nonceStr,
              'package': data.package,
              'signType': 'MD5',
              'paySign': data.paySign,

              'success': function (res) {

                //如果支付成功，做响应处理
                if (res.errMsg.indexOf('ok') >= 0) {
                  wx.navigateTo({
                    url: '/pages/mypay/mypay'
                  });
                }

              },
              'fail': function (res) {

                if (res.errMsg.indexOf('cancel') >= 0) {    //如果用户取消支付
                  netUtil.showAlertDialog("提示", "你已经取消支付", false, "确定", null, null);
                } else {       //如果支付失败
                  netUtil.showAlertDialog("提示", "支付失败", false, "确定", null, null);
                }

              }
            })

          } else {
            if (resp.message != undefined && resp.message != null && resp.message != '') {
              netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
            } else {
              netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
            }
          }
        },
        onError: function (msgCanShow, code, hiddenMsg) {
          console.log("fail");
          netUtil.hideLoadingDialog(that);
          netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
        }
      }).send();
    });

  },

  //查询欠费单汇总
  countDebtAccount: function () {
    var that = this;
    var params = {
      method: 'wechat.wisen.account.debt.count',
      openId: app.globalData.userInfo.openid,
      
    };

    netUtil.buildRequest(that, '/wisen/api/wechat/ret', params, {
      onPre: function () {
        netUtil.showLoadingDialog(that);
      },
      onSuccess: function (resp) {

        netUtil.hideLoadingDialog(that);

        //如果请求成功
        if (resp.ret == app.globalData.respSuccessCode) {

          var data = resp.data;

          var accountIds = data.accountIds;//单据号拼接
          var accountAccess = data.accountAccess;//入库费用
          var accountStorage = data.accountStorage;//存储费用
          var totalPrice = data.totalPrice;//总价格

          that.setData({
            accountIds: accountIds,
            accountAccess: accountAccess,
            accountStorage: accountStorage,
            totalPrice: totalPrice,
            disposablePrice:0
          });
        } else {
          if (resp.message != undefined && resp.message != null && resp.message != '') {
            netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
          } else {
            netUtil.showAlertDialog("提示", "网络请求失败", false, "确定", null, null);
          }
        }
      },
      onError: function (msgCanShow, code, hiddenMsg) {
        console.log("fail");
        netUtil.hideLoadingDialog(that);
        netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
      }
    }).send();

  },

  //查询仓租明细的返回结果
  getAccountStorageQueryResult: function () {
    var that = this;
    var params = {
      method: 'wechat.wisen.account.storage.query',
      openId: app.globalData.userInfo.openid,
      data: {
        accountCode: that.data.accountCode
      }
    };

    netUtil.buildRequest(that, '/wisen/api/wechat/ret', params, {
      onPre: function () {
        netUtil.showLoadingDialog(that);
      },
      onSuccess: function (resp) {
        //如果请求成功
        if (resp.ret == app.globalData.respSuccessCode) {

          var data = resp.data;
          that.setData({
            accountStorageQueryResult: data
          });

        } else {
          netUtil.showAlertDialog("提示", resp.message, false, "确定", null, null);
        }
      },
      onError: function (msgCanShow, code, hiddenMsg) {
        console.log("fail");
        netUtil.hideLoadingDialog(that);
        netUtil.showAlertDialog("提示", msgCanShow, false, "确定", null, null);
      }
    }).send();
  }


});